package cool.qingqing.error;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.error.ErrorAttributes;
import org.springframework.boot.web.servlet.error.ErrorController;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import cool.qingqing.result.Result;


@RestController
public class Error implements ErrorController {

    
    // 默认请求地址
    private static final String ERROR_PATH = "/error";​
    @Autowired
    private ErrorAttributes errorAttributes;

    /**
     * web页面异常请求处理
     * @param request
     * @return
     */
    @RequestMapping(value = ERROR_PATH, produces = MediaType.TEXT_HTML_VALUE)
    public String handlePageError(HttpServletRequest request) {
        LOG.info("进入异常跳转");
        Integer statusCode = getStatus(request);
        switch (statusCode) {
            case 404:
                LOG.info("404异常跳转");
                return "error/404";
            case 403:
                LOG.info("403异常跳转");
                return "error/403";
            case 500:
                LOG.info("500异常跳转");
                return "error/500";
            default:
                LOG.info("默认异常跳转");
                return "error/404";
        }

    }

    /**
     * 获取状态码
     * @param request
     * @return
     */
    public Integer getStatus(HttpServletRequest request) {
        Integer statusCode = (Integer) request.getAttribute("javax.servlet.error.status_code");
        if (statusCode == null) {
            return 500;
        }
        return  statusCode;
    }

}